package com.wz.jiangsu.main.td;

import org.springframework.util.StopWatch;

import java.sql.*;

public class TDengineQuery {
    private static Connection getConnection() throws SQLException {
//        String jdbcUrl = "jdbc:TAOS-RS://10.25.10.196:6041/device?user=root&password=taosdata";
        String jdbcUrl = "jdbc:TAOS-RS://127.0.0.1:6041/device?user=root&password=taosdata";
        return DriverManager.getConnection(jdbcUrl);
    }

    private static void printRow(ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            String value = rs.getString(i);
            System.out.print(value);
            System.out.print("\t");
        }
        System.out.println();
    }

    private static void printColName(ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            String colLabel = meta.getColumnLabel(i);
            System.out.print(colLabel);
            System.out.print("\t");
        }
        System.out.println();
    }

    private static void processResult(ResultSet rs) throws SQLException {
        printColName(rs);
        while (rs.next()) {
            printRow(rs);
        }
    }

    private static void queryData() throws SQLException {
        try (Connection conn = getConnection()) {
            try (Statement stmt = conn.createStatement()) {
                StopWatch stopWatch = new StopWatch();

                stopWatch.start("统计 count(*) 时间");
                ResultSet rs = stmt.executeQuery("SELECT count(*) FROM sensor_1 ");
                stopWatch.stop();

                stopWatch.start("统计 count(*) 时间 带有 ts 的过滤条件");
                stmt.executeQuery("SELECT count(*) FROM sensor_1 where ts >=\"2024-07-15T02:57:26.505-04:00\" ");
                stopWatch.stop();

                stopWatch.start("统计 MIN(cpu_1m_avg) 查询所有数据的时间");
                stmt.executeQuery("SELECT MIN(cpu_1m_avg) FROM DEVICE.sensor_1;");
                stopWatch.stop();

                stopWatch.start("统计 MIN(cpu_1m_avg) 带有 ts 的过滤条件");
                stmt.executeQuery("SELECT MIN(cpu_1m_avg) FROM DEVICE.sensor_1 WHERE ts >=\"2024-07-15T02:57:26.505-04:00\"");
                stopWatch.stop();

                stopWatch.start("统计 MAX(cpu_1m_avg) 查询所有数据的时间 ");
                stmt.executeQuery("SELECT MAX(cpu_1m_avg) FROM DEVICE.sensor_1 ");
                stopWatch.stop();

                stopWatch.start("统计 MAX(cpu_1m_avg) 带有 ts 的过滤条件");
                stmt.executeQuery("SELECT MAX(cpu_1m_avg) FROM DEVICE.sensor_1 WHERE ts >=\"2024-07-15T02:57:26.505-04:00\"");
                stopWatch.stop();

                stopWatch.start("统计 SUM(cpu_1m_avg) 查询所有数据的时间 ");
                stmt.executeQuery("SELECT SUM(cpu_1m_avg) FROM DEVICE.sensor_1 ");
                stopWatch.stop();

                stopWatch.start("统计 SUM(cpu_1m_avg) 带有 ts 的过滤条件");
                stmt.executeQuery("SELECT SUM(cpu_1m_avg) FROM DEVICE.sensor_1 WHERE ts >=\"2024-07-15T02:57:26.505-04:00\"");
                stopWatch.stop();

                stopWatch.start("统计 AVG(cpu_1m_avg) 查询所有数据的时间 ");
                stmt.executeQuery("SELECT AVG(cpu_1m_avg) FROM DEVICE.sensor_1 ");
                stopWatch.stop();

                stopWatch.start("统计 AVG(cpu_1m_avg) 带有 ts 的过滤条件");
                stmt.executeQuery("SELECT AVG(cpu_1m_avg) FROM DEVICE.sensor_1 WHERE ts >=\"2024-07-15T02:57:26.505-04:00\"");
                stopWatch.stop();


                stopWatch.start("开始插入 insert");
                stmt.executeQuery("INSERT INTO sensor_1 VALUES (now, 'device_1', 0.5, 1024, 2048, -60, 0.01, 85, 22.5, 55.3, 1013.2, 0.05, 0.02, 30);");
                stopWatch.stop();


                int count = 0;
                while (rs.next()) {

                    count++;
                }
                System.out.println("测试" + stopWatch.prettyPrint());
//                System.out.println("stopWatch.prettyPrint() = " + stopWatch.prettyPrint());
                System.out.println("a >>>>> " + count);

                processResult(rs);
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        queryData();
    }
}

// possible output:
// avg(voltage)	location
// 222.0	California.LosAngeles
// 219.0	California.SanFrancisco
